package UI;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import threads.BaseRunnableBCBS;
import Logic.RoundMath;
import Logic.SQLReader;
import UI.BaseModels.BCBSPanel;
import UI.BaseModels.HeaderLabel;
import UI.BaseModels.PurchaseSellButton;
import UI.BaseModels.ReturnToMainMenuButton;

// TODO: Auto-generated Javadoc
/**
 * The Class PurchasePanel.
 */
public class PurchasePanel extends BCBSPanel {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -7223467477448458263L;
	
	/** The amount field. */
	private JTextField amountField;
	
	/** The confirm button. */
	private JButton confirmButton;
	
	private double maxpurchase;

	/**
	 * Instantiates a new purchase panel.
	 */
	public PurchasePanel() {
		
		maxpurchase = 100;

		HeaderLabel header = new HeaderLabel("Purchase BCBS");
		this.add(header);

		confirmButton = new PurchaseSellButton("Purchase");
		confirmButton.setLocation(90, 191);
		confirmButton.addActionListener(new BtnPurchaseBCBSActionListener());
		this.add(confirmButton);

		amountField = new JTextField();
		amountField.setHorizontalAlignment(SwingConstants.CENTER);
		amountField.setFont(new Font("Tahoma", Font.PLAIN, 15));
		amountField.setBounds(90, 134, 124, 25);
		amountField.addActionListener(new BtnPurchaseBCBSActionListener());
		this.add(amountField);
		amountField.setColumns(10);

		ReturnToMainMenuButton rtmm = new ReturnToMainMenuButton();
		this.add(rtmm);
	}

	private class BtnPurchaseBCBSActionListener implements ActionListener {
		
	
		public void actionPerformed(ActionEvent arg0) {
			purchaseBCBS();
		}
	}

	/**
	 * Clears fields and resets the Confirm-button. Overrides superclass method.
	 */
	@Override
	public void clearFields() {
		feedbackLabel.setText("");
		amountField.setText("");
		confirmButton.setEnabled(true);
	}

	/**
	 * Enables the Confirm-button based on parameter specified.
	 * @param value
	 * If true enables the button, if false deactivates it.
	 */
	public void enableButton(boolean value) {
		if (value) {
			confirmButton.setEnabled(true);
		} else {
			confirmButton.setEnabled(false);
		}
	}

	/**
	 * Creates a new Runnable of the type purchaseBCBS, sets it to the current
	 * runnable for the panel, places it in its own thread and runs it.
	 */
	private void purchaseBCBS() {
		runnable = new PurchaseBCBSRunnable();
		Thread t = new Thread(runnable);
		t.start();
	}


	private class PurchaseBCBSRunnable extends BaseRunnableBCBS {
		
		/**
		 * Transfers the specified (by user interface) of BCBS to the account of the current user.
		 */
		public void run() {
			
			boolean valuecheck = false;
			boolean invalidinput = false;
			double roundedvalue = 0;
			try {
				roundedvalue = RoundMath.round(
						Double.parseDouble(amountField.getText().trim()), 2);
				amountField.setText(String.valueOf(roundedvalue));
				if (roundedvalue > 0 && roundedvalue <= maxpurchase) {
					valuecheck = true;
				}
			} catch (NumberFormatException e) {
				feedback("Please input a valid amount!", false);
				enableButton(true);
				invalidinput = true;
			}
			if (valuecheck && !stop) {

				try {
					SQLReader reader = new SQLReader();
					enableButton(false);
					clearFeedback();
					reader.performTransaction(UI.MainFrame.currentUser.getId(),
							1, roundedvalue, "Purchased " + roundedvalue
									+ " BCBS from CBS Bank");
					if (!stop) {
						UI.MainFrame.switchToPanel(UI.MainFrame.main_menu,null);
						UI.MainFrame.main_menu.feedback("", "Purchased "
								+ roundedvalue + " BCBS!", true);
						clearFields();
					}

				} catch (SQLException e) {
					if (!stop) {
						feedback("Connection error. Try again!", false);
						enableButton(true);
					}
				}

			} else if (roundedvalue <= 0 && !invalidinput && roundedvalue <= maxpurchase){
				feedback("Amount must be larger than 0!", false);
				enableButton(true);
			} else if (!invalidinput && roundedvalue > maxpurchase) {
				feedback("Amount cannot be larger than "+String.valueOf(maxpurchase)+"!", false);
				enableButton(true);
			}
		}
	}

}
